package com.af.dao.flight.impl;

import java.sql.SQLException;
import java.util.Map;

import org.apache.log4j.Logger;

import com.af.common.DBHelper;
import com.af.common.Log4jXmlConfig;
import com.af.common.PropertiesManager;
import com.af.dao.flight.IFlightNotice;
import com.af.dao.pay.IPayToApi;
import com.af.dao.pay.PayEntity;
import com.af.dao.pay.impl.PayToApi;
import com.af.entity.flight.FlightOrder;

public class FlightNotice implements IFlightNotice {

	Logger LOG = Log4jXmlConfig.getInstance().returnLogger(FlightNotice.class);

	@Override
	public void changeSignNotify() throws Exception {
		// TODO Auto-generated method stub

	}

	/**
	 * 退款
	 */
	@Override
	public void orderRefund(Map map) {
		// TODO Auto-generated method stub
		String orderId = map.get("orderNo").toString();

		String status = map.get("status").toString();

		// 退款金额
		double amount = Double.parseDouble(map.get("amount").toString());

		// 是否在线退款[0:线下,1:线上]
		String isOnline = map.get("isOnline").toString();

		if (isOnline.equals("1")) {

			IPayToApi aliPay = new PayToApi();

			PayEntity pe = new PayEntity();

			pe.setOrderid(orderId);

			FlightOrder fo = getFlightOrder(orderId);

			double payTotal = Double.parseDouble(fo.getPayTotao().toString());

			// 退款扣除手续费
			double poundage = payTotal - amount;

			double refund = Double.parseDouble(fo.getOrderTotal().toString())
					- poundage;

			pe.setTot_trans_amt(returnRefundTotal(refund));

			pe.setSub_order_flag("N");

			pe.setSub_order_num(0);

			pe.setBuyer_id(fo.getMemberNum().toString());

			pe.setOrder_desc("");

			pe.setNotify_url(PropertiesManager.getInstance().returnValue(
					"FLIGHT_NOTICE"));

			aliPay.REFUND(pe);

		}

	}

	/**
	 * 精确金额到分，取整数
	 * 
	 * @param amt
	 * @return
	 */
	private int returnRefundTotal(double amt) {

		int refund = (int) amt * 100;

		return refund;

	}

	/**
	 * 出票
	 */
	@Override
	public void orderTicket(Map map) {
		// TODO Auto-generated method stub
		// 添加新出票通知记录

		String sql = "{call addOrderTicket(?,?,?,?)}";

		Object[] args = new Object[4];

		args[0] = map.get("service");

		args[1] = map.get("orderNo");

		args[2] = map.get("passengers");

		args[3] = map.get("ticketsNo");

		update(sql, args);

	}

	@Override
	public void remarkNotify(Map map) {
		// TODO Auto-generated method stub
		String sql = "{call addRemarkNotify(?,?,?)}";

		Object[] args = new Object[3];

		args[0] = map.get("service");

		args[1] = map.get("orderNo");

		args[3] = map.get("remarkType");

		update(sql, args);
	}

	/**
	 * 修改票号
	 */
	@Override
	public void updateTickets() throws Exception {
		// TODO Auto-generated method stub

	}

	/**
	 * 获取机票订单信息
	 * 
	 * @param orderid
	 * @return
	 */
	private FlightOrder getFlightOrder(String orderid) {

		FlightOrder fo = null;
		String sql = new StringBuffer(
				"SELECT * FROM Plane_Order WHERE OrderNo='").append(orderid)
				.append("'").toString();
		try {
			DBHelper dh = DBHelper.getInstance();

			dh.setBeanClass(FlightOrder.class);

			fo = (FlightOrder) dh.query(sql);

			dh.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			LOG.error(new StringBuffer("获取数据信息异常，订单号：").append(orderid).append(
					"\n").append("执行语句：").append(sql).append("\n"));
			LOG.error(sql);
			e.printStackTrace();

		} finally {

			return fo;

		}
	}
	
	private void update(String sql, Object... args) {

		DBHelper dh = DBHelper.getInstance();

		try {
			dh.update(sql, args);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			LOG.error(new StringBuffer("操作数据时异常").append(e.getMessage())
					.append("\n").append("执行语句：").append(sql).append("\n")
					.append("参数信息：").append(args));
			
			LOG.error(sql);
			
			e.printStackTrace();
		}
		dh.close();
	}

}
